import 'dart:convert';

import 'package:flutter/widgets.dart';
import 'package:liuxlcommon/util/sp_utils.dart';
import 'package:love_newf/app/com/base/router_manage.dart';
import 'package:love_newf/app/com/model/app_config_model.dart';
import 'package:love_newf/app/com/model/login_user_info_model.dart';
import 'package:love_newf/app/com/model/user_info_model.dart';
import 'package:love_newf/app/com/model/user_setting_model.dart';

import '../http/http_request.dart';

class L {
  L._();

  static LoginUserInfoModel? _loginUserInfoModel;
  static AppConfigModel? _appConfig;
  static UserSettingModel? _userSetting;

  static LoginUserInfoModel? get loginUserInfo {
    var json1 = SpUtils().getStorage("loginUserInfo");
    if (json1 != null) {
      _loginUserInfoModel = LoginUserInfoModel.fromJson(json.decode(json1));
    } else {
      _loginUserInfoModel = null;
    }
    return _loginUserInfoModel;
  }

  static late UserInfoModel userInfo;

  static Future<AppConfigModel> get appConfig async =>
      _appConfig ??= await HttpRequest()
          .post<AppConfigModel>('/auth/initData', null, autoToast: false);

  static Future<UserSettingModel> get userSetting async =>
      _userSetting ??= await HttpRequest().post<UserSettingModel>(
          '/api/user/getUserSetting', null,
          autoToast: false);

  // 销毁
  static void reset() {
    _appConfig = null;
    _loginUserInfoModel = null;
  }
}

class MyFactory {}
